<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.items.class.php');


class BaseItemRelationsBase extends BaseItemBase {
	//relations
	//	relation
	//		key=name
	//		name,type,table,pkey,fkey,fields,filter,params,config,class,file
	//			type:11,1N,N1,NN
	var $relations = array();
	
	//constructor
	function BaseItemRelationsBase() {
		parent::BaseItemBase();
	}
	
	//methods
	function getRelation($item) {
		if( is_array($this->relations) ) {
			foreach($this->relations as $rk => $relation) {
				if( $this->_valid_relation($relation) == false ) {
					continue;
				}
				$relation = $this->_filter_relation($relation);
				//
				$r_name = isset($relation['name']) ? $relation['name'] : $rk;
				$r_type = $relation['type'];
				//
				$r_table = $relation['table'];
				$r_prefix = $relation['prefix'];
				$r_pkey = $relation['pkey'];
				$r_fkey = $relation['fkey'];
				$r_fields = $relation['fields'];
				//
				$r_filter = $relation['filter'];
				$r_params = $relation['params'];
				$r_config = $relation['config'];
				//
				$r_class = $relation['class'];
				$r_file = $relation['file'];
				
				//
				if( isset($relation['file']) && isset($relation['class']) ) {
					$this->loadclass($relation['file']);
					$relation_obj = new $relation['class']();
					if( isset($relation['config']) ) {
						$relation_obj->_init($relation['config']);
					}
				} else {
					$relation_obj = new BaseItemItemsBase();
					$relation_obj->table = $r_table;
					$relation_obj->prefix = $r_prefix;
				}
				
				//
				$id = $item[$this->_pk()];
				
				//
				$relation_obj->filter_fields = $r_fields;
				
				//
				if( $r_type == '1-1' ) {
					$fkey = '=' . $relation['fkey'];
					$r_data = $relation_obj->getItemBy($id, $fkey);
				} elseif( $r_type == 'N-1' ) {
					$fkey = $relation['fkey'];
					$fkid = $item[$fkey];
					$r_data = $relation_obj->getItem($fkid);
				} elseif( $r_type == '1-N' ) {
					$fkey = '=' . $relation['fkey'] . ':=';
					$relation_obj->filter_fields = array($fkey);
					$filter = array($fkey => $id);
					
					$r_data = $relation_obj->getItems($filter);
				} elseif( $r_type == 'N-N' ) {
					$relation_obj->relation_key = $relation['relation_key'];
					$relation_obj->table_relation = $relation['table_relation'];
					$relation_obj->table_relation_key = $relation['table_relation_key'];
					$relation_obj->table_relation_params = array($relation['table_relation_fkey'] => $id);
					
					$r_data = $relation_obj->getItems();
				}
				
				//
				$item['relations'][$rk] = $r_data;
			}
		}
		
		return $item;
	}
	
	function getRelations($items) {
		foreach($items as $k => $item) {
			$items[$k] = $this->getRelation($item);
		}
		
		return $items;
	}
	
	function processRelation($item) {
		if( is_array($this->relations) ) {
			foreach($this->relations as $rk => $relation) {
				if( $this->_valid_relation($relation) == false ) {
					continue;
				}
				$relation = $this->_filter_relation($relation);
				//
				$r_name = isset($relation['name']) ? $relation['name'] : $rk;
				$r_type = $relation['type'];
				//
				$r_table = $relation['table'];
				$r_prefix = $relation['prefix'];
				$r_pkey = $relation['pkey'];
				$r_fkey = $relation['fkey'];
				$r_fields = $relation['fields'];
				//
				$r_filter = $relation['filter'];
				$r_params = $relation['params'];
				$r_config = $relation['config'];
				//
				$r_class = $relation['class'];
				$r_file = $relation['file'];
				
				//
				if( isset($relation['file']) && isset($relation['class']) ) {
					$this->loadclass($relation['file']);
					$relation_obj = new $relation['class']();
					if( isset($relation['config']) ) {
						$relation_obj->_init($relation['config']);
					}
				} else {
					$relation_obj = new BaseItemItemsBase();
					$relation_obj->table = $r_table;
					$relation_obj->prefix = $r_prefix;
				}
				
				//
				$id = $item[$this->_pk()];
				
				//
				$relation_obj->filter_fields = $r_fields;
				
				//
				if( $r_type == '1-1' ) {
					$fkey = '=' . $relation['fkey'];
					$r_data = $relation_obj->getItemBy($id, $fkey);
				} elseif( $r_type == 'N-1' ) {
					$fkey = $relation['fkey'];
					$fkid = $item[$fkey];
					$r_data = $relation_obj->getItem($fkid);
				} elseif( $r_type == '1-N' ) {
					$fkey = '=' . $relation['fkey'] . ':=';
					$relation_obj->filter_fields = array($fkey);
					$filter = array($fkey => $id);
					
					$r_data = $relation_obj->getItems($filter);
				} elseif( $r_type == 'N-N' ) {
					$relation_obj->relation_key = $relation['relation_key'];
					$relation_obj->table_relation = $relation['table_relation'];
					$relation_obj->table_relation_key = $relation['table_relation_key'];
					$relation_obj->table_relation_params = array($relation['table_relation_fkey'] => $id);
					
					$r_data = $relation_obj->getItems();
				}
				
				//
				$item['relations'][$rk] = $r_data;
			}
		}
		
		return $item;
	}
	
	//private
	function _valid_relation($relation) {
		if( !isset($relation['type']) ) {
			return false;
		}
		return true;
	}
	
	function _filter_relation($relation) {
		$relation['type'] = strtoupper($relation['type']);
		$relation['fields'] = empty($relation['fields']) ? '*' : $relation['fields'];
		
		return $relation;
	}
}
//
?>